package com.irishud.hudnavi;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.util.Log;

public class StepingMotor {
	private static final String TAG = "StepingMotor";
	private static final String STEPMOTORFLE = "/sys/bus/platform/drivers/commom2phase/rwreg";
	public String readlinuxfile(String filepath){
		String str = "";
		try{
			File readFile = new File(filepath);
	        FileReader fr = new FileReader(readFile);
	        try{
	        	char [] buffer = null;
	        	int v1 = fr.read();
	        	int v2 = fr.read();
	        	int v3 = fr.read();
	        	int v4 = fr.read();
	        	if(("" + v1 + v2 + v3 + v4).equals("70826969"))
	        		return "free";
	        	else
	        		return "busy";
			} catch (IOException e) {
				e.printStackTrace();
			}
        } catch (FileNotFoundException e){
        	e.printStackTrace();
        }
		return "busy";
	}
	
	public boolean writelinuxfile(String filepath,String str) {
		File writeFile = new File(filepath);
        FileWriter fw;
        
        String state="";
        do {
            state = readlinuxfile(filepath);
            Log.d(TAG,"k.log state:" + state);
            if(state.equals("busy")) {
            	try {
                    Thread.sleep(500);
                } catch (Exception e) {
                      e.printStackTrace();
                }
            } else {
            	break;
            }
        } while(true);
        
        try {
            fw = new FileWriter(writeFile);
            fw.write(str); 
            fw.close();
        } catch (IOException e) {
              e.printStackTrace();
              return false;
        }
        return true;
	}
	
	public void testlinuxfilerw() {
		thread_test.run();
	}
    Thread thread_test = new Thread(new Runnable() {
    	String path = "/sys/bus/platform/drivers/commom2phase/rwreg";
    	String [] str = {"2 1","1 50",
    					"2 0","1 A0",
    					"2 1","1 80",
    					"2 0","1 60",
    					"2 1","1 40",
    					"2 0","1 10",};
        public void run() {
            FileTool mFT = new FileTool();
            int i = 0;
            while (true) {
                try {
                    Log.d(TAG,"k.log cycle:" + i);
                    mFT.writelinuxfile(path,str[i++]);
                    Thread.sleep(1);
                    mFT.writelinuxfile(path,str[i++]);
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(i > 11)
                	i = 0;
            }
        }
    });
    
    private boolean nMotorBusy = false;
    private float nextangle = 0;
    private float prveangle = 0;
    String [] MOTORDIR = {"2 1","2 0"};
    public boolean isStepMotroBusy() {
    	String state = readlinuxfile(STEPMOTORFLE);
    	nMotorBusy = (state.equals("busy"))?true:false;
    	return nMotorBusy;
    }
    public void setStepMotorAngle(float angle) {
        if(isStepMotroBusy()) {
    		return;
        } 
        if((angle < -180.0) || (angle > 180.0)) {
    		Log.d(TAG,"Step Motoe angle is not surport");
        	return;
        }
    	if(prveangle == angle) {
    		Log.d(TAG,"Step Motoe angle is not changed");
    		return;
    	}
    	nextangle = angle;
    	Thread thread_setnextangle = new Thread(new Runnable() {
            public void run() {
                FileTool mFT = new FileTool();
                while (true) {
                    try {
                        float diffangle = nextangle - prveangle;
                    	int dir = (diffangle > 0)?1:0;
                        mFT.writelinuxfile(STEPMOTORFLE,MOTORDIR[dir]);
                        Thread.sleep(1);
                        int step = (int) (diffangle / 1.8);
                        mFT.writelinuxfile(STEPMOTORFLE,"1 " + step);
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    	thread_setnextangle.run();
    }
}
